// 头像组件 方便以后独立，增加裁剪之类的功能

import { uploadUsingPost } from '@/services/backend/fileInfoController';
import { UploadOutlined } from '@ant-design/icons';
import { Button, message, Upload } from 'antd';
import React from 'react';
import useStyles from './index.style';
import {useModel} from "@umijs/max";

interface Props {
  avatar: string;
}


const AvatarView: React.FC<Props> = (props) => {
  const { avatar } = props;
  const { styles } = useStyles();
  // const {initialState,setInitialState}  = useModel("@@initialState");

  const handleBeforeUpload = async (value: any) => {
    console.log(value)
    // try {
    //   //action="http://localhost:8080/file/upload"
    //   const res = await uploadUsingPost({}, value.file);
    //
    //   if (res.code === 0) {
    //
    //     //修改用户的avatar信息
    //     const avatar = res.data;
    //
    //     setInitialState({
    //       ...initialState,
    //       currentUser: {
    //         avatar
    //       },
    //     });
    //
    //     message.success('上传成功');
    //   }
    // } catch (error: any) {
    //   message.error(error.message);
    // }
  };


  return (
    <>
      <div className={styles.avatar_title}>头像</div>
      <div className={styles.avatar}>
        <img src={avatar} alt="avatar" />
      </div>
      <Upload showUploadList={false} customRequest={handleBeforeUpload}>
        <div className={styles.button_view}>
          <Button>
            <UploadOutlined />
            更换头像
          </Button>
        </div>
      </Upload>
    </>
  );
};

export default AvatarView;
